package com.wing.warehouse.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.warehouse.model.entity.MoveDownWarehouse;
import com.wing.warehouse.model.request.MoveDownWarehouseForm;
import com.wing.warehouse.model.response.MoveDownWarehouseVO;
import com.wing.warehouse.model.response.MoveGoodsListVO;
import com.wing.warehouse.service.MoveDownWarehouseItermService;
import com.wing.warehouse.service.MoveDownWarehouseService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-调拨任务管理", tags = "#/warehouse/moveDownWareHouse")
@RestController("MoveDownWarehouseController")
@RequestMapping("warehouse/admin/moveDownWareHouse")
@RequiredArgsConstructor
public class MoveDownWarehouseController {

    private final MoveDownWarehouseService moveDownWarehouseService;
    private final MoveDownWarehouseItermService moveDownWarehouseItermService;
    @Autowired
    private EnterpriseClient enterpriseClient;
    
    @ApiOperation(value = "查询调拨任务列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<MoveDownWarehouseVO> list(
        @RequestParam(defaultValue = "1") Integer page
        , @RequestParam(defaultValue = "10") Integer limit
        , @RequestParam(defaultValue = "create_time") String sort
        , @RequestParam(defaultValue = "desc") String order
        , String moveDownNumber
        , String outWarehouseId
        , String inWarehouseId
        , String creatorName
        , String startCreateTime
        , String endCreateTime
        , Integer status
    ){
        Page<MoveDownWarehouse> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("moveDownNumber", moveDownNumber);
        params.put("outWarehouseId", outWarehouseId);
        params.put("inWarehouseId", inWarehouseId);
        params.put("creatorName", creatorName);
        params.put("startCreateTime", startCreateTime);
        params.put("endCreateTime", endCreateTime);
        params.put("status", status);
        //排序
        params.put("sort",StrUtil.toUnderlineCase(sort));
        params.put("order",order);
        IPage<MoveDownWarehouseVO> list = moveDownWarehouseService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "获取当前仓库的料箱信息", notes = "查询")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/getNowLiaoXiangInfo")
    public JsonResult getNowLiaoXiangInfo(String outWarehouseId,String warehouseShelfSpaceId) {
        Map queryParam=new HashMap();
        queryParam.put("outWarehouseId",outWarehouseId);
        queryParam.put("warehouseShelfSpaceId",warehouseShelfSpaceId);
        return JsonResult.success(moveDownWarehouseService.getNowLiaoXiangInfo(queryParam));
    }


    @ApiOperation(value = "获取当前仓库的库位信息", notes = "查询")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/getNowKuWeiInfo")
    public JsonResult getNowKuWeiInfo(String inWarehouseId) {
        Map queryParam=new HashMap();
        queryParam.put("inWarehouseId",inWarehouseId);
        return JsonResult.success(moveDownWarehouseService.getNowKuWeiInfo(queryParam));
    }

    @ApiOperation(value = "添加调拨任务", notes = "保存")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(MoveDownWarehouseForm.Add.class) @RequestBody MoveDownWarehouseForm moveDownWarehouseForm) {
        if (moveDownWarehouseService.add(moveDownWarehouseForm) > 0) {
            return JsonResult.success("添加成功！");
        }
        return JsonResult.fail("添加失败！");
    }

    @ApiOperation(value = "查询下货物料列表", notes = "搜索")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
        ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
        ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
        ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/getGoodsList")
    public PageResult<MoveGoodsListVO> getGoodsList(
        @RequestParam(defaultValue = "1") Integer page
        ,@RequestParam(defaultValue = "10") Integer limit
        ,@RequestParam(defaultValue = "create_time") String sort
        ,@RequestParam(defaultValue = "desc") String order
        ,String outWarehouseId
    ){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        if (StringUtils.isNotBlank(outWarehouseId)) {
            queryParam.put("outWarehouseId", outWarehouseId);
        }
        IPage<MoveGoodsListVO> list = moveDownWarehouseService.getGoodsList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "调拨任务详情", notes = "调拨任务-详情")
    @GetMapping("detail/{id}")
    public JsonResult<MoveDownWarehouseVO> getDetailById(@PathVariable("id") Long id) {
        MoveDownWarehouseVO moveDownWarehouseVO = moveDownWarehouseService.getDetailById(id);
        return JsonResult.success(moveDownWarehouseVO);
    }

    @ApiOperation(value = "修改移库下货表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(MoveDownWarehouseForm.Update.class) @RequestBody MoveDownWarehouseForm moveDownWarehouseForm) {
        MoveDownWarehouse moveDownWarehouse = moveDownWarehouseService.getById(moveDownWarehouseForm.getId());
        if(moveDownWarehouse == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
            .setEditable(MoveDownWarehouse.class)
            .setIgnoreError(true)
            .setIgnoreNullValue(true);
        BeanUtil.copyProperties(moveDownWarehouseForm, moveDownWarehouse, copyOptions);
        if (moveDownWarehouseService.updateById(moveDownWarehouse)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "修改移库下货表", notes = "修改")
    @PutMapping("/updateMapById")
    public JsonResult updateMapById(@RequestBody Map<String, Object> param) {
        MoveDownWarehouse moveDownWarehouse = moveDownWarehouseService.getById(Long.parseLong(param.get("id").toString()));
        if(moveDownWarehouse == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
            .setEditable(MoveDownWarehouse.class)
            .setIgnoreError(true)
            .setIgnoreNullValue(true);
        BeanUtil.copyProperties(param, moveDownWarehouse, copyOptions);
        if (moveDownWarehouseService.updateById(moveDownWarehouse)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }
    
    @ApiOperation(value = "调拨任务取消", notes = "调拨任务-取消")
    @GetMapping("cancel/{id}")
    public JsonResult<MoveDownWarehouseVO> cancel(@PathVariable("id") Long id) {
        if (moveDownWarehouseService.cancel(id) > 0) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }
}
